# coding: utf-8

"""
    # @author:  zsj
    # @file:    abase.py
    # @time:    2024/8/12 18:53
"""

import json
from collections import namedtuple


class TableFunc:
    def __repr__(self):
        # return str(self._to_dict())
        pass

    def __str__(self):
        return str(self._to_dict())

    def _to_dict(self):
        _dict = self.__dict__.copy()
        if "_sa_instance_state" in _dict:
            del _dict["_sa_instance_state"]
        return _dict
    
    def _to_json(self):
        return self._to_dict()

    # def to_dict(self):
    #  return {c.name: getattr(self, c.name) for c in self.__table__.columns}

    def to_model(self, json_str):
        return json.loads(json_str, object_hook=self.json_to_model)

    def json_to_model(self, json_str):
        return namedtuple(self.__class__.__name__, json_str.keys())(*json_str.values())
